package com.kangzhan.utils;

import com.jfinal.kit.Kv;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;


/**
 * @desc 验证码服务
 * @author george
 */
@Service
public class CaptchaService {

	@Autowired
	private RedisUtil redisDao;

	private final int DEFAULT_FONT = 1; // 默认字体
	private final int DEFAULT_LEN = 2; // 默认长度
	private final int DEFAULT_WIDTH = 110; // 默认宽度
	private final int DEFAULT_HEIGHT = 40; // 默认高度

	public void out(HttpServletResponse response, String captchaId) throws IOException, FontFormatException {
		out(DEFAULT_LEN, response, captchaId);
	}

	public String toBase64(String captchaId) throws IOException, FontFormatException {
		return toBase64(DEFAULT_LEN, captchaId);
	}

	public void out(int len, HttpServletResponse response, String captchaId) throws IOException, FontFormatException {
		out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, response, captchaId);
	}

	public String toBase64(int len, String captchaId) throws IOException, FontFormatException {
		return toBase64(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, captchaId);
	}

	public void out(int width, int height, int len, HttpServletResponse response, String captchaId)
			throws IOException, FontFormatException {
		out(width, height, len, DEFAULT_FONT, response, captchaId);
	}

	public String toBase64(int width, int height, int len, String captchaId) throws IOException, FontFormatException {
		return toBase64(width, height, len, DEFAULT_FONT, captchaId);
	}

	public void out(HttpServletResponse response, String captchaId, int font) throws IOException, FontFormatException {
		out(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LEN, font, response, captchaId);
	}

	public String toBase64(String captchaId, int font) throws IOException, FontFormatException {
		return toBase64(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LEN, font, captchaId);
	}

	public Kv toKvBase64(int font) throws IOException, FontFormatException {

		ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LEN);
		arithmeticCaptcha.setFont(font);
		return toKvBase64(arithmeticCaptcha);

	}

	public void out(int len, int font, HttpServletResponse response, String captchaId)
			throws IOException, FontFormatException {
		out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, font, response, captchaId);
	}

	public String toBase64(int len, int font, String captchaId) throws IOException, FontFormatException {
		return toBase64(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, font, captchaId);
	}

	public void out(int width, int height, int len, int font, HttpServletResponse response, String captchaId)
			throws IOException, FontFormatException {
		ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(width, height, len);
		arithmeticCaptcha.setFont(font);
		out(arithmeticCaptcha, response, captchaId);
	}

	public String toBase64(int width, int height, int len, int font, String captchaId)
			throws IOException, FontFormatException {
		ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(width, height, len);
		arithmeticCaptcha.setFont(font);
		return toBase64(arithmeticCaptcha, captchaId);
	}

	public void out(Captcha captcha, HttpServletResponse response, String captchaId) throws IOException {
		setHeader(response);
		redisDao.set(captchaId, captcha.text().toLowerCase(), 60);
		captcha.out(response.getOutputStream());
	}

	public String toBase64(Captcha captcha, String captchaId) throws IOException {
		redisDao.set(captchaId, captcha.text().toLowerCase(), 60);
		return captcha.toBase64();
	}

	public Kv toKvBase64(Captcha captcha) throws IOException {
		return new Kv().set("base64", captcha.toBase64()).set("result", captcha.text().toLowerCase());
	}

	/**
	 * @desc 验证验证码
	 * @param code    用户输入的验证码
	 * @param request HttpServletRequest
	 * @return 是否正确
	 */
	public boolean ver(String code, String captchaId) {
		if (code != null) {
			String captcha = (String) redisDao.get(captchaId);
			return code.trim().toLowerCase().equals(captcha);
		}
		return false;
	}

	/**
	 * @desc 清除验证码
	 * @param request HttpServletRequest
	 */
	public void clear(String captchaId) {
		redisDao.del(captchaId);
	}

	/**
	 * 设置相应头
	 *
	 * @param response HttpServletResponse
	 */
	public void setHeader(HttpServletResponse response) {
		response.setContentType("image/gif");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
	}

}
